package studentinformationmanagementsystem.dao.impl;

import studentinformationmanagementsystem.dao.AdminDao;
import studentinformationmanagementsystem.dao.BaseDao;
import studentinformationmanagementsystem.entity.Admin;

import java.util.List;
/**
 * @author 苗庆
 * @version 1.0
 */
public class AdminDaoImpl extends BaseDao implements AdminDao {


    /**
     * 添加管理员
     * @param admin
     * @return
     */
    @Override
    public int insert(Admin admin) {
        String sql="INSERT INTO `T_admin`(`username`,`password`,`type`,`IsDelete`)VALUES(?,?,?,?)";
        return super.update(sql,admin.getUsername(),admin.getPassword(),admin.getType(),admin.getIsDelete());
    }

    /**
     * 根据ID更新管理员信息
     * @param admin
     * @return
     */
    @Override
    public int update(Admin admin) {
        String sql="UPDATE `T_admin` SET `username`=?,`password`=?,`type`=?,`IsDelete` = ? WHERE `adminid` =?";
        return super.update(sql,admin.getUsername(),admin.getPassword(),admin.getType(),admin.getIsDelete(),admin.getAdminid());
    }


    /**
     * 根据ID删除管理员信息
     * @param admin
     * @return
     */
    @Override
    public int delete(Admin admin) {
        String sql="DELETE FROM `T_admin` WHERE `adminid`=? AND `IsDelete`=0";
        return super.update(sql,admin.getAdminid());
    }


    /**
     * 查询所有用户
     * @return 所有管理员信息
     */
    @Override
    public List<Admin> queryAll(Admin admin) {
        String sql="SELECT * FROM `T_admin` WHERE `IsDelete` = ?";
        return super.queryForList(Admin.class,sql,admin.getIsDelete());
    }


    /**
     *
     * @param admin 管理员ID
     * @return
     */
    @Override
    public Admin queryById(Admin admin) {
        String sql = "SELECT * FROM `T_admin` WHERE `adminid` = ? AND `IsDelete`=0";
        return super.queryForOne(Admin.class, sql, admin.getAdminid());
    }

    /**
     *
     * @param admin 账号
     * @return
     */
    @Override
    public Admin queryByUsername(Admin admin) {
        String sql = "SELECT * FROM `T_admin` WHERE `username` = ?  AND `IsDelete`=0 ";
        return super.queryForOne(Admin.class, sql, admin.getUsername());
    }


    /**
     * 根据用户名和密码查询用户信息
     * @param
     * @return 用户信息
     */
    @Override
    public Admin queryByUsernameAndPassword(Admin admin) {
        String sql="SELECT * FROM `t_admin` WHERE  `username` = ? AND `password` = ? AND `IsDelete`=0";
        return super.queryForOne(Admin.class,sql,admin.getUsername(),admin.getPassword());
    }

    /**
     *
     * @return
     */
    @Override
    public Integer queryForPageTotalCount() {
        String sql = "SELECT COUNT(1) FROM `t_admin` WHERE `IsDelete`=0 ";
        Number count = (Number) queryForSingleValue(sql);
        return count.intValue();
    }


    /**
     *
     * @param begin    起始记录行数
     * @param pageSize 每页展示数量
     * @return
     */
    @Override
    public List<Admin> queryForPageItems(int begin, int pageSize) {
        String sql = "SELECT * FROM `t_admin` WHERE `IsDelete`=0 LIMIT ?, ?";
        return super.queryForList(Admin.class, sql, begin, pageSize);
    }

}
